package edu.api.list_api;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * List : 有序 、可重复的集合
 * ArrayList： 动态数组
 */
public class Demo {

    public static void main(String[] args) {
        dedup();
    }

    public static void testStu_list(){
        Product pd1 = new Product(4, "木糖醇", 10);
        Product pd2 = new Product(5, "洗发水", 12);
        Product pd3 = new Product(3, "热水壶", 49);
        List<Product> list = new ArrayList(); // 创建集合
        list.add(pd1);
        list.add(pd2);
        list.add(pd3);
        System.out.println("*************** 商品信息 ***************");

        list.sort(new Comparator<Product>() {
            @Override
            public int compare(Product o1, Product o2) {
                return (int)(o1.getPrice() - o2.getPrice());
            }
        });
    }


    /**
     * 集合元素的去重
     * stream().distinct();
     *
     */
    public static void dedup(){
        List<Integer> list = Arrays.asList(100,102,98,78,65,56,100);
        //将集合转成stream对象
        Stream<Integer> stream = list.stream();
        //去重distinct()
        List<Integer> list1 = stream.distinct().collect(Collectors.toList());
        System.out.println("list1 = " + list1);
        //过滤
        list1 = list1.stream().filter(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) {
                return integer >= 60;
            }
        }).collect(Collectors.toList());
        System.out.println("list1 = " + list1);
    }


    /**
     * ArrayList元素的排序
     * sort : 实现Comparator接口
     */
    public static void testSort(){
        List<Integer> list = Arrays.asList(100,102,98,78,65,56,100);
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        System.out.println("list = " + list);
    }

    public static void testStream(){
        List<Integer> list = Arrays.asList(100,102,98,78,65,56);
        list.stream().forEach(new Consumer<Integer>() {
            @Override
            public void accept(Integer i) {
                System.out.println("i = " + i);
            }
        });
    }

    public static void testForeach2(){
        List<Integer> list = Arrays.asList(100,102,98,78,65,56);
        list.forEach(new Consumer<Integer>() {
            /**
             * @param i  集合中的元素
             */
            @Override
            public void accept(Integer i) {
                System.out.println("i = " + i);
            }
        });
    }

    public static void testIterator(){
        //迭代器
        List<Integer> list = Arrays.asList(100,102,98,78,65,56);
        Iterator<Integer> iterator = list.iterator();
        //判断迭代器中是否还有元素
        while (iterator.hasNext()) {
            //获取接下来的元素
            Integer i = iterator.next();
            System.out.println("i = " + i);
        }

    }


    public static void testForeach(){
        List<Integer> list = Arrays.asList(100,102,98,78,65,56);
        for (Integer i : list) {
            System.out.println("i = " + i);
        }
    }

    /**
     * fori循环
     */
    public static void testFor(){
        //将数组转成List集合
        List<Integer> list = Arrays.asList(100,102,98,78,65,56);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }


    /**
     * 移除成绩不及格的
     */
    public static void testRemoveIf(){
        ArrayList<Integer> list = new ArrayList<>();
        list.add(100);
        list.add(98);
        list.add(78);
        list.add(66);
        list.add(50);
        list.add(66);
        list.add(58);
        list.add(51);
        //断言   条件
        Predicate<Integer> predicate = new Predicate<Integer>() {
            /**
             * 条件
             * @param i 集合中的元素
             * @return
             */
            @Override
            public boolean test(Integer i) {
                return i < 60;
            }
        };
        boolean b = list.removeIf(predicate);
        System.out.println("b = " + b);
        System.out.println("list = " + list);
    }



    /**
     * ArrayList集合的元素操作
     */
    public static void operate(){
        ArrayList<Integer> list = new ArrayList<>();
        int size = list.size();
        System.out.println("当前集合的元素个数：" + size);
        //添加数据
        list.add(100);    //下标0
        list.add(98);    //下标1
        list.add(78);    //下标2
        list.add(66);    //下标3
        list.add(50);    //下标4
        list.add(66);    //下标5
        System.out.println(list);
        //获取元素
        Integer i = list.get(0);
        System.out.println("i = " + i);
        //修改数据   根据下标修改
        Integer j = list.set(1, 99);
        System.out.println("list.get(1) = " + list.get(1));
//        list.set(2,78);
//        System.out.println("list.get(2) = " + list.get(2));
        //判断是否包含某个元素
        boolean result = list.contains(99);
        System.out.println("result = " + result);
        //根据元素找下标
        int k = list.indexOf(66);
        System.out.println("k = " + k);
        int m = list.lastIndexOf(66);
        System.out.println("m = " + m);
        //移除元素
        Integer integer = new Integer(200);
        boolean n = list.remove(integer);
        System.out.println("n = " + n);
        Integer o = list.remove(4);
        System.out.println("o = " + o);
        ArrayList<Integer> list2 = new ArrayList<>();
        list2.add(100);
        list2.add(78);
        list2.add(200);
        boolean b = list.removeAll(list2);
        System.out.println("b = " + b);
        System.out.println("list = " + list);
        //集合转数组
        Object[] array = list.toArray();

    }


    /**
     * ArrayList创建方式
     */
    public static void create(){
        //创建一个ArrayList 对象  集合  此时默认保存的数据类型是Object，初始容量是10
        ArrayList list = new ArrayList();
        //在创建list的同时指定元素类型   <元素数据类型>
        ArrayList<String> names_list = new ArrayList<>();
        //创建list同时指定初始容量
        ArrayList<Integer> scores_list = new ArrayList<>(60);
        //多态
        List<Student> studentList = new ArrayList<>();
        //将一个collection对象包装成list
        Set set = new HashSet();
        ArrayList<String> arrayList = new ArrayList<>(set);

    }


}
